class Solution {
public:
    bool isValid(string s) 
    {
        stack<char> tmp;
        for(int i=0;i<s.size();i++)
        {
            if(s[i]=='('||s[i]=='['||s[i]=='{')
            {
                tmp.push(s[i]);
            }
            else
            {
                if(tmp.empty())
                {
                    return false;
                }
                char dial=tmp.top();
                if((dial=='('&&s[i]!=')')||(dial=='['&&s[i]!=']')||(dial=='{'&&s[i]!='}'))
                {
                    return false;
                }
                tmp.pop();
            }
        }
        if(!tmp.empty())
        return false;
        return true;
    }
};
